import cv2
import tkinter
from tkinter import Button
from PIL import Image, ImageTk
from random import shuffle
import numpy as np
from time import sleep
from pathlib import Path


WIDTH = HEIGHT = 700
SIZE = 6    #必须为偶数
UNIT = WIDTH // SIZE
level={'简单':6,'普通':10,'困难':16,'地狱':20}

win = tkinter.Tk()
win.geometry(str(WIDTH) + 'x' + str(HEIGHT))
win.title('熊猫连连看')

# LEVEL=None
# def level_choose(text):
#     LEVEL=level[text]
#
# simple_button = Button(win,text="简单", command=lambda: level_choose("简单"))
# normal_button = Button(win,text="普通", command=lambda: level_choose("普通"))
# hard_button = Button(win,text="困难", command=lambda: level_choose("困难"))
# hell_button = Button(win,text="地狱", command=lambda: level_choose("地狱"))
#
# simple_button.pack()
# normal_button.pack()
# hard_button.pack()
# hell_button.pack()
# print(LEVEL)

canvas = tkinter.Canvas(win, width = WIDTH, height = HEIGHT, bg = 'white')

random_mat = [[i for i in range(SIZE)] for _ in range(SIZE)]
random_mat = [i for i in range(SIZE)]*SIZE
shuffle(random_mat)
random_mat=np.array(random_mat).reshape(SIZE,SIZE)
zero_mat=np.zeros((SIZE+2,SIZE+2),dtype=int)
zero_mat[1:SIZE+1,1:SIZE+1]=random_mat
zero_mat[0,]=zero_mat[-1,]=zero_mat[:,0]=zero_mat[:,-1]=-1
random_mat=zero_mat
print(random_mat)

def img_resize():
    cur_path = Path(__file__)
    root_path = cur_path.parent.parent
    img_root_path = root_path.joinpath(Path('pictures'))
    # print('img_root_path:',img_root_path)
    imgs = []
    for i in range(SIZE):
        img = Image.open(img_root_path.joinpath(Path('panda'+str(i)+'.jpg')))
        img = img.resize((UNIT, UNIT))
        img = ImageTk.PhotoImage(img)
        imgs.append(img)
    return imgs


imgs = img_resize()
for i in range(1,SIZE+1):
    for j in range(1,SIZE+1):
        img = imgs[random_mat[i, j]]
        print(img)
        canvas.create_image((j-1) * UNIT+UNIT//2, (i-1) * UNIT+UNIT//2, image = img)

canvas.pack()


def if_connected(x, y):
    '''
    判断x,y两点是否连通
    '''
    # global random_mat
    if random_mat[x]!=random_mat[y] or x==y:
        return
    # 相贴
    if x[0] == y[0] and abs(x[1] - y[1]) == 1 or x[1] == y[1] and abs(x[0] - y[0]) == 1:
        print(1)
        return True
    # 直线
    if x[0]==y[0] and x[1]>y[1] or x[1]==y[1] and x[0]>y[0]:
        x,y=y,x
    if x[0] == y[0] and (random_mat[x[0], x[1] + 1:y[1]] == -1).all() or x[1] == y[1] and (
            random_mat[x[0] + 1:y[0], x[1]] == -1).all():
        print(2,random_mat[x[0], x[1] + 1:y[1]])
        return True

    # 一弯
    def one_corner(x, y):
        if x[0] > y[0]:
            x, y = y, x
        if x[1] < y[1]:
            if (random_mat[x[0], x[1] + 1:y[1] + 1] == -1).all() and (random_mat[x[0] + 1:y[0], y[1]] == -1).all() \
                    or (random_mat[y[0], x[1]:y[1]] == -1).all() and (random_mat[x[0] + 1:y[0], x[1]] == -1).all():
                return True
        elif x[1] > y[1]:
            if (random_mat[x[0], y[1]:x[1]] == -1).all() and (random_mat[x[0] + 1:y[0], y[1]] == -1).all() \
                    or (random_mat[y[0], y[1] + 1:x[1] + 1] == -1).all() and (
                    random_mat[x[0] + 1:y[0], x[1]] == -1).all():
                return True

    if one_corner(x, y):
        print(3)
        return True
    # 二弯,点的气遍历加一弯判断
    if x[0]>y[0]:
        x,y=y,x
    # 上
    cnt = 1
    if x[0] > 0:
        while x[0] - cnt >= 0 and random_mat[x[0] - cnt, x[1]] == -1:
            xup = (x[0] - cnt, x[1])
            oc = one_corner(xup, y)
            if oc:
                print(4.1)
                return True
            cnt += 1
    # 下
    cnt = 1
    if x[0] < SIZE + 1:
        while x[0] + cnt <= SIZE + 1 and random_mat[x[0] + cnt, x[1]] == -1:
            xbottom = (x[0] + cnt, x[1])
            oc = one_corner(xbottom, y)
            if oc:
                print(4.2)
                return True
            cnt += 1
    # 左
    cnt = 1
    if x[1] > 0:
        while x[1] - cnt >= 0 and random_mat[x[0], x[1] - cnt] == -1:
            xleft = (x[0], x[1] - cnt)
            oc = one_corner(xleft, y)
            if oc:
                print(4.3)
                return True
            cnt += 1
    # 右
    cnt = 1
    if x[1] < SIZE + 1:
        print('hh:',x,SIZE+1)
        while x[1] + cnt <= SIZE + 1 and random_mat[x[0], x[1] + cnt] == -1:
            xright = (x[0], x[1] + cnt)
            oc = one_corner(xright, y)
            if oc:
                print(4.4)
                return True
            cnt += 1


click_first = False
firstX, firstY = None, None
def left_button(event):
    global click_first,firstX,firstY
    click_first = True if not click_first else False
    print(click_first)
    x, y = event.y // UNIT, event.x // UNIT
    if click_first:
        firstX, firstY = x, y
    print(x, y)
    canvas.create_rectangle(y * UNIT, x * UNIT, (y + 1) * UNIT, (x + 1) * UNIT,outline = 'yellow', width = 1)
    if not click_first:
        if if_connected((firstX+1,firstY+1),(x+1,y+1)):
            canvas.create_rectangle(firstY * UNIT, firstX * UNIT, (firstY + 1) * UNIT, (firstX + 1) * UNIT,fill='white')
            canvas.create_rectangle(y * UNIT, x * UNIT, (y + 1) * UNIT, (x + 1) * UNIT, fill='white')
            canvas.create_rectangle(firstY * UNIT, firstX * UNIT, (firstY + 1) * UNIT,(firstX + 1) * UNIT, outline = 'black', width = 1)
            canvas.create_rectangle(y * UNIT, x * UNIT, (y + 1) * UNIT, (x + 1) * UNIT, outline = 'black', width = 1)
            random_mat[firstX+1,firstY+1]=random_mat[x+1,y+1]=-1
        else:
            canvas.create_rectangle(firstY * UNIT, firstX * UNIT, (firstY + 1) * UNIT, (firstX + 1) * UNIT, outline = 'black', width = 1)
            canvas.create_rectangle(y * UNIT, x * UNIT, (y + 1) * UNIT, (x + 1) * UNIT, outline = 'black', width = 1)
        print(random_mat)


win.bind('<Button-1>', left_button)

win.mainloop()

